package com.xinwei.leetcode.二叉树;


import sun.reflect.generics.tree.Tree;

import java.util.LinkedList;

// https://leetcode-cn.com/problems/minimum-absolute-difference-in-bst/
public class _530_二叉搜索树的最小绝对差 {
    // 中序遍历 1、全部保存成数组 2、直接保存前一个的值与当前值比较
    int min = Integer.MAX_VALUE;
    int pre = -1;
    public int getMinimumDifference(TreeNode root) {
        inorderTraversal(root);
        return min;
    }

    private void inorderTraversal(TreeNode node) {
        if (node == null) return;
        inorderTraversal(node.left);
        if (pre == -1) {
            pre = node.val;
        }else {
            min = Math.min(node.val - pre, min);
            pre = node.val;
        }
        inorderTraversal(node.right);
    }


}
